package com.zrx.springboot.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrx.springboot.entity.Book;
import com.zrx.springboot.entity.CartVo;
import com.zrx.springboot.entity.User;
import com.zrx.springboot.entity.UserCartVo;
import com.zrx.springboot.entity.enums.Category;
import com.zrx.springboot.service.BookService;
import com.zrx.springboot.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 图书控制器
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private CartService cartService;
    @RequestMapping("/index")
    public String index(HttpServletRequest request,Model model){
        Cookie[] cookies = request.getCookies();
        User user = new User();
        if(cookies != null){
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("username")) {
                user.setUsername(cookie.getValue());
            }
            if(cookie.getName().equals("password")) {
                user.setPassword(cookie.getValue());
            }
        }
        }
        model.addAttribute("cookies", user);
            return "index";
    }
    /**
     * 获取图书信息
     */
    @RequestMapping("/getBookData")
    public String gerBookData(Model model,Integer page,Integer category){
        //mybatis puls分页
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category);
        IPage<Book> iPage = bookService.page(new Page<>(page,4),queryWrapper);
        model.addAttribute("bookList",iPage.getRecords());
        model.addAttribute("pre", iPage.getCurrent()-1);
        model.addAttribute("next", iPage.getCurrent()+1);
        model.addAttribute("cur", iPage.getCurrent());
        model.addAttribute("last", iPage.getPages());
        model.addAttribute("category",category);


        return "bookData";
    }

    /**
     *根据名字模糊查找
     */
    @RequestMapping("/findbookname")
    private String findbookname(String findbookname,Model model){
        System.out.println("--------------");
        System.out.println(findbookname);
        System.out.println("--------------");
        model.addAttribute("findbookname",findbookname);
        return "books_listname";
    }
    /**
     * 图书列表页
     */
    @RequestMapping("/bookList")
    public String bookList(String category,Model model){
        model.addAttribute("category",category);
        return "books_list";
    }
    /**
     * 模糊获取图书列表数据
     */
    @RequestMapping("/getBooknameListData")
    public String getBooknameListData(String findbookname,Model model,Integer page,Integer pageSize){
        System.out.println("--------------");
        System.out.println(findbookname);
        System.out.println("--------------");
        //mybatis puls分页
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        Page<Book> bookPage = new Page<>(page, pageSize);
        queryWrapper.like("name",findbookname);
        IPage<Book> iPage = bookService.page(bookPage,queryWrapper);
        model.addAttribute("bookList",iPage.getRecords());
        model.addAttribute("cur", iPage.getCurrent());//当前页
        model.addAttribute("pages", iPage.getPages());//总共页
        model.addAttribute("findbookname",findbookname);
        model.addAttribute("pageSize", pageSize);
        return "booksListData";
    }
    /**
     * 获取图书列表数据
     */
    @RequestMapping("/getBookListData")
    public String getBookListData(String category,Model model,Integer page,Integer pageSize){
        //mybatis puls分页
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category);
        IPage<Book> iPage = bookService.page(new Page<>(page,pageSize),queryWrapper);
        model.addAttribute("bookList",iPage.getRecords());
        model.addAttribute("cur", iPage.getCurrent());//当前页
        model.addAttribute("pages", iPage.getPages());//总共页
        model.addAttribute("category",category);
        model.addAttribute("pageSize", pageSize);
        return "booksListData";
    }
    /**
     * 图书详情
     */
    @RequestMapping("/detail")
    public String detail(Integer id, Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
     if (user!=null){
        List<CartVo> cartVos = cartService.findCartListByUser(user.getId());
        //将用户的购物车信息放在session
        UserCartVo userCartVo = cartService.wrapperCart(cartVos);
        session.setAttribute("userCartInfo", userCartVo);
     }
        Book book = bookService.getById(id);
        model.addAttribute("book", book);
        return "details";
    }


}
